package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.Task;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class TaskDao {
	private DbHelp<Task> db = new DbHelp<Task>();
	
	public List<Task> selectAllByGoalId(int id){
		String sql="select id,taskname,content,state,begtime,endtime,createtime,level,rate,t_user_id,t_goal_id from t_task where t_goal_id = ? ";
		return db.exeSelectForList(sql, new PackgeTask(),id);
	}

	public int saveTask(Task task) {
		String sql = "insert into t_task(taskname,content,state,begtime,endtime,createtime,level,rate,t_user_id,t_goal_id)values(?,?,?,?,?,?,?,?,?,?)";
		return db.executeUpdateSql(sql, task.getTaskName(),task.getContent(),task.getState(),task.getBegTime(),task.getEndTime(),task.getCreateTime(),task.getLevel(),task.getRate(),task.getUserId(),task.getGoalId());
		
	}
	public Task selectTaskById(int id) {
		String sql="select id,taskname,content,state,begtime,endtime,createtime,level,rate,t_user_id,t_goal_id from t_task where id = ? "; 
		return db.exeSelectForObject(sql, new PackgeTask(), id);
	}
	private class PackgeTask implements ResultSetPackge<Task>{

		public Task packgeResultSet(ResultSet rs) throws SQLException {
				Task task = new Task();
				task.setId(rs.getInt("id"));
				task.setBegTime(rs.getString("begtime"));
				task.setContent(rs.getString("content"));
				task.setCreateTime(rs.getString("createtime"));
				task.setEndTime(rs.getString("endtime"));
				task.setGoalId(rs.getInt("t_goal_id"));
				task.setLevel(rs.getString("level"));
				task.setRate(rs.getString("rate"));
				task.setState(rs.getString("state"));
				task.setTaskName(rs.getString("taskname"));
				task.setUserId(rs.getInt("t_user_id"));
				return task;
		
		}
		
		
		
	}
	public int update(Task task) {
		String sql = "update t_task set taskname=?,content=?,state=?,begtime=?,endtime=?,createtime=?,level=?,rate=?,t_user_id=?,t_goal_id=? where id=?";
		return db.executeUpdateSql(sql, task.getTaskName(),task.getContent(),task.getState(),task.getBegTime(),task.getEndTime(),task.getCreateTime(),task.getLevel(),task.getRate(),task.getUserId(),task.getGoalId(),task.getId());
		
	}
}
